package com.rgz.service.WgService.impl;

import java.util.List;
import java.util.Map;

import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.rgz.mapper.CarinsuranceidMapper;
import com.rgz.mapper.CaseDiscribeMapper;
import com.rgz.mapper.CaseoneMapper;
import com.rgz.mapper.PolicyMapper;
import com.rgz.mapper.UserInsuredMapper;
import com.rgz.mapper.UserMapper;
import com.rgz.pojo.Carinsuranceid;
import com.rgz.pojo.CarinsuranceidExample;
import com.rgz.pojo.CaseDiscribe;
import com.rgz.pojo.Caseone;
import com.rgz.pojo.Policy;
import com.rgz.pojo.PolicyExample;
import com.rgz.pojo.User;
import com.rgz.pojo.UserExample;
import com.rgz.pojo.UserInsured;
import com.rgz.pojo.UserInsuredExample;
import com.rgz.pojo.UserExample.Criteria;
import com.rgz.service.WgService.UserService;

@Service
public class UserServiceImpl implements UserService{
	@Autowired
	private UserMapper userMapper;
	@Autowired
	private CaseDiscribeMapper caseDiscribeMapper;
	@Autowired
	private CaseoneMapper caseoneMapper;
	@Autowired
	private CarinsuranceidMapper carinsuranceidMapper;
	@Autowired
	private PolicyMapper policyMapper;
	@Autowired
	private UserInsuredMapper userInsuredMapper;
	

	@Override
	public User UserLogin(String phone) {
		
		UserExample example = new UserExample();
		Criteria criteria = example.createCriteria();
		criteria.andUserphoneEqualTo(phone);
		List<User> list = userMapper.selectByExample(example);
		if(list!=null && list.size()>0){
			return list.get(0);
		}else{
			User user = new User();
			user.setUserphone(phone);
			userMapper.insert(user);
			
			return user;
		}
	}
	@Transactional
	public Boolean CaseAndCaseDiscribeByNo(Caseone case1, CaseDiscribe caseDiscribe) {
		int i=0;
		int j=0;
		int beging=0;
		try {
			PolicyExample policyExample = new PolicyExample();
			com.rgz.pojo.PolicyExample.Criteria createCriteria = policyExample.createCriteria();
			createCriteria.andIdentityEqualTo(caseDiscribe.getIdentity());
			List<Policy> byExample = policyMapper.selectByExample(policyExample);
			
			if(byExample!=null && byExample.size()>0){
				CarinsuranceidExample carinsuranceidExample = new CarinsuranceidExample();
				com.rgz.pojo.CarinsuranceidExample.Criteria createCriteria2 = carinsuranceidExample.createCriteria();
				createCriteria2.andVinEqualTo(caseDiscribe.getCarnumber());
				List<Carinsuranceid> selectByExample = carinsuranceidMapper.selectByExample(carinsuranceidExample);
				if(selectByExample!=null && selectByExample.size()>0){
					beging=1;
				}
			}
			
			
			
			i = caseoneMapper.insert(case1);
			j = caseDiscribeMapper.insert(caseDiscribe);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(i>0 && j>0 && beging>0){
			return true;
		}
		return false;
	}

	@Override
	public Map selectByStarts(String phone) {
		// 根据电话查询用户
		int i=0;
		int j=0;
		UserExample example = new UserExample();
		Criteria usercriteria = example.createCriteria();
		usercriteria.andUserphoneEqualTo(phone);
		List<User> listuser = userMapper.selectByExample(example);
		User user = new User();
		userMapper.insert(user);
		Map<String, Object> map = new HashedMap();
		if (listuser != null && listuser.size() > 0) {
			user = listuser.get(0);
			if (user.getDfPolicyno() == null ||"".equals(user.getDfPolicyno())) {
				return null;
			} else {
				// 查询默认的绑定的单号
				String policyno = user.getDfPolicyno();
				
				// 代维护------
				PolicyExample policyExample = new PolicyExample();
				com.rgz.pojo.PolicyExample.Criteria createCriteria = policyExample.createCriteria();
				createCriteria.andPolicynoEqualTo(policyno);
				List<Policy> policyList = policyMapper.selectByExample(policyExample);

				if (policyList != null && policyList.size() > 0) {
					Policy policy = policyList.get(0);
					map.put("policy", policy);
					i=1;
					
				}
				CarinsuranceidExample carinsuranceidExample = new CarinsuranceidExample();
				com.rgz.pojo.CarinsuranceidExample.Criteria createCriteria2 = carinsuranceidExample.createCriteria();
				createCriteria2.andPolicynoEqualTo(policyno);
				List<Carinsuranceid> ListCarinsuranceid = carinsuranceidMapper.selectByExample(carinsuranceidExample);

				if (ListCarinsuranceid != null && ListCarinsuranceid.size() > 0) {
					Carinsuranceid carinsuranceid = ListCarinsuranceid.get(0);
					map.put("carinsuranceid", carinsuranceid);
					j=1;
				}
				if(i>0&&j>0){
					return map;
				}else{
					return null;
				}
				
			}
		}
		return null;
	}
	@Override
	public String selectByIdcare(String idcare) {
		PolicyExample policyExample = new PolicyExample();
		com.rgz.pojo.PolicyExample.Criteria createCriteria = policyExample.createCriteria();
		createCriteria.andIdentityEqualTo(idcare);
		createCriteria.andInsuranceidEqualTo(1);
		List<Policy> policys = policyMapper.selectByExample(policyExample);
		
		if(policys != null && policys.size()>0){
			String policyno = policys.get(0).getPolicyno();
			return policyno;
		}
		return null;
	}
	
	
	
	@Override
	public Integer idbang(String idcare, String phone) {
		int i = 0;
		PolicyExample policyExample = new PolicyExample();
		com.rgz.pojo.PolicyExample.Criteria createCriteria = policyExample.createCriteria();
		createCriteria.andIdentityEqualTo(idcare);
		UserInsuredExample userInsuredExample = new UserInsuredExample();
		com.rgz.pojo.UserInsuredExample.Criteria createCriteria2 = userInsuredExample.createCriteria();
		createCriteria2.andPoweridEqualTo(phone);
	
		
		// 获取身份证为idcare的全部保单
		List<Policy> policyList = policyMapper.selectByExample(policyExample);
		if (policyList != null && policyList.size() > 0) {
			for (Policy policy : policyList) {
				//存在关联
				List<UserInsured> userinList = userInsuredMapper.selectByExample(userInsuredExample);
				if (userinList != null && userinList.size() > 0) {
					//根据电话查询连接表 中是否有关联
					for (UserInsured userInsured : userinList) {
						// 判断是否存在 已关联的
						if (userInsured.getPowerid() != policy.getPolicyno()) {
							UserInsured userin = new UserInsured();
							userin.setUserphone(phone);
							userin.setPowerid(policy.getPolicyno());
							i = userInsuredMapper.insert(userin);
						}

					}
				//不存关联
				} else {
					
					UserInsured userin = new UserInsured();
					userin.setUserphone(phone);
					userin.setPowerid(policy.getPolicyno());
					i = userInsuredMapper.insert(userin);
					
					UserExample userExample = new UserExample();
					Criteria createCriteria3 = userExample.createCriteria();
					createCriteria3.andUserphoneEqualTo(phone);
					
					// 查询是否有默认的
					List<User> userList = userMapper.selectByExample(userExample);
					if (userList != null && userList.size() > 0) {
						//获取用户对象
						User user3 = userList.get(0);
						//用户对象中 没有添加默认字段
						if (user3.getDfPolicyno() == null || "".equals(user3.getDfPolicyno())) {
							
							user3.setDfPolicyno(policy.getPolicyno());
							
							UserExample userExample1 = new UserExample();
							Criteria createCriteria4 = userExample1.createCriteria();
							createCriteria4.andUserphoneEqualTo(phone);
							userMapper.updateByExampleSelective(user3, userExample1);
						}

					}

				}

			}

		}
		return i;
	}	
		
}
